<template>
	<view :class="styleType == 'col'?'goodsitem':'goodsitem row'" @click="toPage('/pages/shop/detail?id='+item.id)">
		<view class="imgbox" :style="styleType== 'row'?{width:imgsize,height:imgsize}:''">
			<view class="no_stock" v-if="item.stock == 0">
				<text>{{$t('index.ysw')}}</text>
			</view>
			<image class="goodslogo" :src="item.cover_path" mode="aspectFit"></image>
		</view>
		<view class="goodsinfo">
			<view class="goodstitle">
				{{item.name}}
			</view>
			<view class="oldprice">
				${{item.market_price}}
			</view>
			<view class="goodsprice">
				<view class="price">
					${{item.price}}
				</view>
				<view class="goods_cz">
					<view @click.stop="collectGoods(item)">
						<uni-icons :size="20" type="heart" v-if="!item.collected"></uni-icons>
						<uni-icons :size="20" type="heart-filled" color="#ff5353" v-else></uni-icons>
					</view>
					<uni-icons :size="22" type="cart"></uni-icons>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		goodsCollect
	} from "@/api/goods.js"
	export default {
		data() {
			return {

			}
		},
		props: {
			collectField: {
				typeof: String,
				default: 'id'
			},
			item: {
				typeof: Object,
				default () {
					return {}
				}
			},
			styleType: {
				typeof: String,
				default () {
					return 'col';
				}
			},
			imgsize: {
				typeof: String,
				default () {
					return '120px';
				}
			}
		},
		methods: {
			toPage(url) {
				uni.navigateTo({
					url
				})
			},
			//收藏商品
			collectGoods(item) {
				goodsCollect({
					goods_id: item[this.collectField]
				}).then(res => {
					if (res.code == 0) {
						this.$emit('collectGoods', item, !item.collected)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.goodsitem {
		width: 100%;
		padding: 5px;
		box-sizing: border-box;
		margin-bottom: 10px;
		transition: .2s all;
		border-radius: 5px;

		&:hover {
			transform: scale(1.02);
			box-shadow: 0 0 10px #ccc;
		}

		.imgbox {
			position: relative;
			width: 100%;
			height: 280px;
			display: flex;
			align-items: center;
			justify-content: center;

			.no_stock {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				background: #ffffffbf;
				z-index: 9;

				text {
					background: #000;
					color: #fff;
					border-radius: 20px;
					padding: 5px 10px;
					font-size: 15px;
				}
			}
		}

		.goodslogo {
			width: 100%;
			height: 100%;
		}

		.goodsinfo {
			.goodstitle {
				box-sizing: border-box;
				margin-bottom: 4px;
				height: 36px;
				overflow: hidden;
				font-weight: normal;
				font-size: 14px;
				color: rgb(0, 0, 0);
				line-height: 18px;
				text-overflow: ellipsis;
				white-space: normal;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}

			.oldprice {
				color: rgb(170, 170, 170);
				text-decoration: line-through;
				font-size: 12px;
			}

			.goodsprice {
				display: flex;
				justify-content: space-between;

				.price {
					--a: min(100%, 100% - 50px);
					color: rgb(255, 83, 83);
					font-weight: unset;
					font-size: 14px;
					text-overflow: ellipsis;
					white-space: normal;
					-webkit-line-clamp: 1;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					overflow: hidden;
					word-break: break-all;
					width: calc(100% - 50px);
					line-height: 24px;
					max-height: unset;
				}

				.goods_cz {
					width: 50px;
					display: flex;
					align-items: center;
					justify-content: space-around;
				}
			}
		}

		&.row {
			display: flex;

			&:hover {
				transform: scale(1.00);
				box-shadow: 0 0 0 0;
			}

			.imgbox {
				width: 120px;
				height: 120px;
			}

			.goodsinfo {
				// width: calc(100% - 120px);
				flex: 1;
				padding-left: 10px;
				box-sizing: border-box;
				position: relative;

				.goods_cz {
					position: absolute;
					bottom: 0;
					right: 0;

					text {
						font-size: 22px;
					}
				}
			}
		}
	}
</style>